package com.example.tongue.main;

import android.os.Bundle;

import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;


import com.example.tongue.ui.FragmentPageAdapterButtom.FragmentPageAdapterBottom;
import com.example.tongue.R;


public class BottomNavDemo extends AppCompatActivity implements
        ViewPager.OnPageChangeListener,
        RadioGroup.OnCheckedChangeListener
{
    //页面常量
    public static final int tongueImageRecognitionPage=0;
    public static final int onlinePhysicianPage=1;
    public static final int personalPage=2;

    private ViewPager ViewPager;
    private RadioGroup bottomNavigation;
    private RadioButton tongueImageRecognition,onlinePhysician,personal;
    private FragmentPageAdapterBottom pagerAdapterBottom;



//    private final SparseArray<Fragment> fragmentList = new SparseArray<>(4);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.bottomnavdemo);
        //控件绑定资源
        ViewPager = findViewById(R.id.FragmentViewPager);

        bottomNavigation=findViewById(R.id.BottomNavigation);
        tongueImageRecognition = findViewById(R.id.TongueImageRecognition);
        onlinePhysician = findViewById(R.id.OnlinePhysician);
        personal = findViewById(R.id.Personal);
        //新建页面适配器
        pagerAdapterBottom = new FragmentPageAdapterBottom(getSupportFragmentManager());

        //绑定监听器
        bottomNavigation.setOnCheckedChangeListener(this);



        //设置页面
        ViewPager.setAdapter(pagerAdapterBottom);
        //设置当前页面
        ViewPager.setCurrentItem(tongueImageRecognitionPage);
        ViewPager.addOnPageChangeListener(this);

//        //RadioGroup.OnCheckedChangeListener 无需设置监听器
//        tongueImageRecognition.setOnClickListener(this);
//        onlinePhysician.setOnClickListener(this);
//        personal.setOnClickListener(this);

    }


//    public void onClick(View v){
//        switch (v.getId()) {
//            case R.id.TongueImageRecognition:
//            {   ViewPager.setCurrentItem(0);
//                tongueImageRecognition.setChecked(true);
//            }
//
//            break;
//            case R.id.OnlinePhysician:
//            {   ViewPager.setCurrentItem(1);
//                onlinePhysician.setChecked(true);
//            }
//
//            break;
//
//            case R.id.Personal:
//            {   ViewPager.setCurrentItem(2);
//                personal.setChecked(true);
//            }
//
//            break;
//        }
//    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.TongueImageRecognition:{
                ViewPager.setCurrentItem(tongueImageRecognitionPage);
            }break;
            case R.id.OnlinePhysician:{
                ViewPager.setCurrentItem(onlinePhysicianPage);
            }break;
            case R.id.Personal:{
                ViewPager.setCurrentItem(personalPage);
            }break;
        }


    }

//重写的ViewPager.OnPageChangeListener的方法
    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {
            if(state==2){
                switch (ViewPager.getCurrentItem()){
                    case tongueImageRecognitionPage:
                    {   tongueImageRecognition.setChecked(true);
                    } break;
                    case onlinePhysicianPage:{
                        onlinePhysician.setChecked(true);
                    }break;
                    case personalPage:{
                        personal.setChecked(true);
                    }break;
                }


            }
    }


}







